<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录" left-arrow />
    <!-- /导航栏 -->

    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginRef">
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        clearable
        required
        v-model="user.mobile"
        :rules="userFormRules.mobile"
      >
        <!--【增加图标】-->
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        name="code"
        placeholder="请输入验证码"
        clearable
        required
        v-model="user.code"
        :rules="userFormRules.code"
      >
        <!--【增加图标】-->
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <!--【增加发送按钮】-->
        <template #button>
          <!--这里注意，要加上native-type,否则也可以实现表单提交-->
          <van-count-down
            v-if="isCountDownShow"
            :time="1000 * 60"
            format="ss s"
            @finish="isCountDownShow = false"
          />

          <van-button
            v-else
            class="send-sms-btn"
            size="normal"
            native-type="button"
            type="success"
            @click="onSendSms"
          >
            发送验证码
          </van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button
          class="login-btn"
          round
          block
          native-type="submit"
          type="success"
          >登录</van-button
        >
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>

<script>
export default {
  name: 'LoginIndex',
  data () {
    return {
      isCountDownShow: false, // 是否展示倒计时
      user: {
        mobile: '17342065909',
        code: '246810'
      },
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^1[3|5|7|8|8]\d{9}$/,
            message: '手机格式错误'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      }
    }
  },
  methods: {
    async onSubmit (values) {
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true, // 禁用背景点击
        duration: 0 // 持续时间，默认 2000，0 表示持续展示不关闭
      })

      try {
        this.$store.dispatch('user/userLogin', values)
        this.$toast.success('登录成功')
        this.$router.push('/')
      } catch (error) {
        console.log('登录失败', error)
        if (error.response.status === 400) {
          this.$toast.fail('手机号或验证码错误')
        } else {
          console.log('登录失败，请稍后重试', error)
          this.$toast.fail('登录失败，请稍后重试')
        }
      }
    },

    async onSendSms () {
      this.isCountDownShow = true
      // 发送验证码
      // 3. 请求发送验证码
      try {
        this.$store.dispatch('user/userSms', this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        // 发送失败，关闭倒计时，显示发送按钮
        this.isCountDownShow = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，请稍后重试')
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    height: 46px;
    line-height: 46px;
    background-color: #07c160;
    font-size: 22px;
    border: none;
    color: #fff;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #1989fa;
      color: #fff;
      border: none;
    }
  }
}
</style>
